<template>
  <div class="main-layout">
    <!-- 侧边导航栏 -->
    <SideNavigation 
      :collapsed="sidebarCollapsed"
      @toggle-collapse="handleToggleCollapse"
    />
    
    <!-- 主内容区域 -->
    <div class="main-content" :class="{ 'sidebar-collapsed': sidebarCollapsed }">
      <!-- 顶部导航栏 -->
      <TopNavigation 
        :sidebar-collapsed="sidebarCollapsed"
        @toggle-sidebar="handleToggleCollapse"
      />
      
      <!-- 面包屑导航 -->
      <BreadcrumbNavigation />
      
      <!-- 页面内容 -->
      <div class="page-content">
        <router-view />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import SideNavigation from '@/components/navigation/SideNavigation.vue'
import TopNavigation from '@/components/navigation/TopNavigation.vue'
import BreadcrumbNavigation from '@/components/navigation/BreadcrumbNavigation.vue'
import { useUserStore } from '@/store/user'

// 侧边栏折叠状态
const sidebarCollapsed = ref(false)
const userStore = useUserStore()

// 切换侧边栏折叠状态
const handleToggleCollapse = () => {
  sidebarCollapsed.value = !sidebarCollapsed.value
}

// 组件挂载时确保获取用户信息
onMounted(async () => {
  if (userStore.isAuthenticated && !userStore.userInfo) {
    try {
      await userStore.getUserInfo()
    } catch (error) {
      console.error('获取用户信息失败:', error)
    }
  }
})
</script>

<style scoped>
.main-layout {
  display: flex;
  height: 100vh;
  background-color: #f5f7fa;
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  margin-left: 240px;
  transition: margin-left 0.3s ease;
  overflow: hidden;
}

.main-content.sidebar-collapsed {
  margin-left: 64px;
}

.page-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  background-color: #f5f7fa;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .main-content {
    margin-left: 0;
  }
  
  .main-content.sidebar-collapsed {
    margin-left: 0;
  }
}
</style>
